package com.quanxiaoha.framework.biz.operationlog.aspect;

import com.quanxiaoha.framework.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Collectors;

@Aspect
@Slf4j
public class ApiOperationLogAspect {

    @Pointcut("@annotation(com.quanxiaoha.framework.biz.operationlog.aspect.ApiOperationLog)")
    public void apioperationLog() { }

    @Around("apioperationLog()")
    public Object doAround(ProceedingJoinPoint joinpoint) throws Throwable {
        // 请求时间
        long startTime = System.currentTimeMillis();

        // 获取请求的类和方法
        String className = joinpoint.getTarget().getClass().getSimpleName();
        String methodName = joinpoint.getSignature().getName();

        // 请求入参
        Object[] args = joinpoint.getArgs();

        //入参json
        String argsJsonStr = Arrays.stream(args).map(toJsonStr()).collect(Collectors.joining(","));

        //功能描述
        String description = getApiOperationLogDescription(joinpoint);
        // 打印请求相关参数
        log.info("====== 请求开始: [{}], 入参: {}, 请求类: {}, 请求方法: {} =================================== ",
                description, argsJsonStr, className, methodName);

        // 执行切点方法
        Object result = joinpoint.proceed();

        // 执行耗时
        long executionTime = System.currentTimeMillis() - startTime;

        // 打印出参等相关信息
        log.info("====== 请求结束: [{}], 耗时: {}ms, 出参: {} =================================== ",
                description, executionTime, JsonUtils.toJsonString(result));

        return result;
    }

    /**
     * 获取注解的描述信息
     * @param joinpoint
     * @return
     */
    private String getApiOperationLogDescription(ProceedingJoinPoint joinpoint) {
        // 1. 从 ProceedingJoinPoint 获取 MethodSignature
        MethodSignature signature = (MethodSignature)joinpoint.getSignature();
        // 2. 使用 MethodSignature 获取当前被注解的 Method
        Method method = signature.getMethod();
        // 3. 从 Method 中提取 LogExecution 注解
        ApiOperationLog apiOperationLog = method.getAnnotation(ApiOperationLog.class);
        // 4. 从 LogExecution 注解中获取 description 属性
        return apiOperationLog.description();


    }
    /**
     * 引用方法，转 JSON 字符串
     * @return
     */
    private Function<Object, String> toJsonStr() {
        return JsonUtils::toJsonString;
    }

}
